﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Model;
using BLL;

/// <summary>
/// 登录按钮
/// </summary>
/// <param name="sender">事件对象</param>
/// <param name="e">事件参数</param>

/// <summary>
/// 用户界面设置 UI层
/// </summary>

 
//在UI层用户进行登录相应操作，此层需要用到业务逻辑层BLL和实体层Model
namespace BookManage
{
    public partial class frmBookManage : Form
    {
        public frmBookManage()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "" || txtUserPwd.Text == "")
            {
                MessageBox.Show("用户名和密码不能为空！", "系统提示");
                return;
            }
            //在实体层Model实例化出一个用户（用到了Model层）
            BookUser u = new BookUser();
            u.UserName = txtUserName.Text;
            u.UserPwd = txtUserPwd.Text;
            //去业务逻辑层BLL形成登录相应操作sql语句（用到了BLL层），若无误则显示图书系统窗口，有误则显示相应提示
            if (UserService.UserLogin(u) == "1")
            {
                FrmMain frm = new FrmMain();
                frm.Show();
                this.Hide();
            }
            else if (UserService.UserLogin(u) == "-1")
                MessageBox.Show("数据库连接出现异常！", "异常提示");
            else
                MessageBox.Show("用户名或密码错误！", "登录错误");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddUser_Click(object sender, EventArgs e)
        {
            frmAddUser frm = new frmAddUser();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BookUser user = frm.user;
                if (BLL.UserService.AddUser(user))
                    MessageBox.Show("注册成功！", "系统提示");
                else
                    MessageBox.Show("注册失败！", "系统提示");
            }
        }
    }
}
